<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/16/016
 * Time: 11:22
 */

namespace app\api\controller;


use app\api\model\Sellerorder;
use app\common\controller\Api;

class Order extends Api
{
    protected $noNeedLogin = ['*'];

    public function getOrders()
    {
        $user_id = $this->request->request('user_id');
        $seller_id = $this->request->request('seller_id');
        $status = $this->request->request('status');

        if (!\app\common\model\User::get($user_id)) {
            $this->error('没有这个用户');
        }

        if (!\app\admin\model\Seller::get($seller_id)) {
            $this->error('没有这个商家');
        }
        if(!$status){
            $status = 0;
        }
        $order = new Sellerorder();
        $result = $order->where('user_id',$user_id)
            ->where('seller_id',$seller_id)
            ->where('status', $status)
            ->select();
        if(!$result){
            $this->error('没有相关的订单');
        }
        $this->success('',$result);
    }

    /**
     * 从购物车购买，创建订单
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        $user_id = $this->request->request('user_id');
        $seller_id = $this->request->request('seller_id');

        if (!\app\common\model\User::get($user_id)) {
            $this->error('没有这个用户');
        }

        if (!\app\admin\model\Seller::get($seller_id)) {
            $this->error('没有这个商家');
        }
        $cartitem = new \app\api\model\Usercartitem();
        $cartitems = $cartitem->where('user_id', $user_id)->where('seller_id', $seller_id)->where('status', 1)->select();
        if (!$cartitems) {
            $this->error('购物车没有任何商品');
        }

        //创建订单
        $ordermodel = new \app\api\model\Sellerorder();
        $result = $ordermodel->where('user_id', $user_id)->where('seller_id', $seller_id)->where('status', 3)->find();
        if ($result) {
            $this->error('当前有未处理的订单，请先处理');
        }
        $result = $ordermodel->allowField(true)->validate(true)->save($_POST);
        if (!$result) {
            $this->error($ordermodel->getError());
        }

        //创建订单项,从购物车购买
        $order_id = $ordermodel->id;

        $totalprice = 0;
        for ($x = 0; $x < count($cartitems); $x++) {
            $model = new \app\api\model\Orderitem();
            $result = $model->allowField(true)->validate(true)->save([
                'order_id' => $order_id,
                'seller_id' => $seller_id,
                'sellerproduct_id' => $cartitems[$x]["sellerproduct_id"],
                'num' => $cartitems[$x]["num"],
                'totalprice' => $cartitems[$x]["price"] * $cartitems[$x]["num"],
                'price' => $cartitems[$x]["price"],
                'productname' => $cartitems[$x]['productname'],
                'image' => $cartitems[$x]['image'],
            ]);

            if (!$result) {
                $this->error($model->getError());
            }
            $totalprice += $cartitems[$x]["price"] * $cartitems[$x]["num"];
        }
        //设置订单总价
        $order = new Sellerorder();
        $result = $order->save(['totalprice' => $totalprice], ['id' => $order_id]);

        if (!$result) {
            $this->error('设置订单总价失败');
        }
        //更改已经生成订单的商品状态
        $result = $cartitem->save(['status' => 2], ['seller_id' => $seller_id, 'user_id' => $user_id]);
        if(!$result){
            $this->error('更改购物车商品状态失败');
        }
        $this->success('成功');

    }

    /**
     * 完成付款，清空购物车
     */
    public function pay()
    {
        //根据订单id获取订单，设置状态已支付
        $id = $this->request->request('id');
        if (!$id) {
            $this->error('订单id不能为空');
        }
        $order = new Sellerorder();
        $result = $order->save(['status' => 0], ['id' => $id]);
        if (!$result) {
            $this->error('更改订单状态失败');
        }
        //根据用户id，商家id，获取购物车中已生成订单但是未支付的，删除
        $user_id = $this->request->request('user_id');
        $seller_id = $this->request->request('seller_id');

        if (!\app\common\model\User::get($user_id)) {
            $this->error('没有这个用户');
        }

        if (!\app\admin\model\Seller::get($seller_id)) {
            $this->error('没有这个商家');
        }
        //在没有支付和取消之前不能产生新的订单
        $cartitem = new \app\api\model\Usercartitem();
        $result = $cartitem->where('user_id', $user_id)->where('seller_id', $seller_id)->where('status', 2)->delete();
        if (!$result) {
            $this->error('更改购物车失败');
        }
        $this->success('成功');
    }

    //取消订单
    public function cancelOrder()
    {
        //删除订单
        $id = $this->request->request('id');
        $user_id = $this->request->request('user_id');
        $seller_id = $this->request->request('seller_id');

        if (!\app\common\model\User::get($user_id)) {
            $this->error('没有这个用户');
        }

        if (!\app\admin\model\Seller::get($seller_id)) {
            $this->error('没有这个商家');
        }
        if (!$id) {
            $this->error('订单id不能为空');
        }
        $order = new Sellerorder();
        $result = $order->where(['id' => $id])->delete();
        if (!$result) {
            $this->error('删除订单失败');
        }
        //设置购物车状态
        $cartitem = new \app\api\model\Usercartitem();
        $result = $cartitem->save(['status' => 1], ['seller_id' => $seller_id, 'user_id' => $user_id, 'status' => 2]);
        if(!$result){
            $this->error('设置购物车状态失败');
        }
        $this->success('成功');
    }

}